package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Task;
import com.hengxinguotong.zhihuichengjian.ui.engineering.reservation.IDragSort;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/27.
 */

public class TaskAdapter extends BaseAdapter implements IDragSort {
    private List<Task> tasks;
    private Context context;

    public TaskAdapter(Context context, List<Task> tasks) {
        this.context = context;
        this.tasks = tasks;
    }

    public void setDatas(List<Task> tasks){
        this.tasks = tasks;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return tasks.size();
    }

    @Override
    public Task getItem(int position) {
        return tasks.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_task, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Task task = tasks.get(position);
        if(null == task.getRemark() || Utils.isEmpty(task.getRemark())){
            holder.content.setText("无");
        }else{
            holder.content.setText(task.getRemark());
        }
        if(null == task.getScheduledPlanName() || Utils.isEmpty(task.getScheduledPlanName())){
            holder.structureTv.setText("无");
        }else{
            holder.structureTv.setText(task.getScheduledPlanName());
        }
        holder.dataTv.setText(task.getCreateTime());
        holder.processerNameTv.setText(task.getProcessorName());
        if(null == task.getProjectCompanyName() || Utils.isEmpty(task.getProjectCompanyName())){
            holder.companyTv.setText("无");
        }else{
            holder.companyTv.setText(task.getProjectCompanyName());
        }
        if ("1".equals(task.getStatus())) {
            holder.stateTv.setText("待受理");
            holder.stateTv.setTextColor(context.getResources().getColor(R.color.color_dd66ff));
            holder.stateIv.setImageResource(R.mipmap.task_daishouli);
        } else if ("2".equals(task.getStatus())) {
            holder.stateTv.setText("处理中");
            holder.stateTv.setTextColor(context.getResources().getColor(R.color.color_1294db));
            holder.stateIv.setImageResource(R.mipmap.task_chulizhong);
        } else if ("3".equals(task.getStatus())) {
            holder.stateTv.setText("待检查");
            holder.stateTv.setTextColor(context.getResources().getColor(R.color.color_5dd7e2));
            holder.stateIv.setImageResource(R.mipmap.task_daijiancha);
        } else if ("4".equals(task.getStatus())) {
            holder.stateTv.setText("检查不通过");
            holder.stateTv.setTextColor(context.getResources().getColor(R.color.color_fc8d43));
            holder.stateIv.setImageResource(R.mipmap.dispatched_workers);
        } else {
            holder.stateTv.setText("已完成");
            holder.stateTv.setTextColor(context.getResources().getColor(R.color.color_71e5a2));
            holder.stateIv.setImageResource(R.mipmap.already_confirmed);
        }
        holder.orderNumTv.setText(task.getOrderNo());
        holder.inspectNameTv.setText(task.getInspectName());
        return convertView;
    }

    @Override
    public void remove(int arg0) {
        tasks.remove(arg0);
        this.notifyDataSetChanged();//不要忘记更改适配器对象的数据源
    }

    @Override
    public void insert(Task item, int arg0) {
        tasks.add(arg0, item);
        this.notifyDataSetChanged();
    }

    static class ViewHolder {
        @BindView(R.id.state_iv)
        ImageView stateIv;
        @BindView(R.id.state_tv)
        HXTextView stateTv;
        @BindView(R.id.content)
        HXTextView content;
        @BindView(R.id.structure_tv)
        HXTextView structureTv;
        @BindView(R.id.processer_name_tv)
        HXTextView processerNameTv;
        @BindView(R.id.company_tv)
        HXTextView companyTv;
        @BindView(R.id.data_tv)
        HXTextView dataTv;
        @BindView(R.id.inspect_name_tv)
        HXTextView inspectNameTv;
        @BindView(R.id.order_num_tv)
        HXTextView orderNumTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
